' Copyright (c) Microsoft Corporation.  All rights reserved.
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web

Namespace EdmMetadataHelpersVB
    ''' <summary>
    ''' A specialized collection of EdmWrapper items that has Thread or Session bound 
    ''' semantics for accessing a Current default collection.
    ''' </summary>
    Public Class EdmWrappers
        Inherits List(Of EdmWrapper)
        ''' <summary>
        ''' Finds an EdmWrapper by name
        ''' </summary>
        Public Function FindByName(ByVal name As String) As EdmWrapper
            Dim matches = From wrapper In Me _
                          Where wrapper.Name = name _
                          Select wrapper

            Return matches.FirstOrDefault()
        End Function

        Private Shared Function GetNewCurrent() As EdmWrappers
            Return New EdmWrappers
        End Function

        Private Shared Function GetSessionBoundCurrent() As EdmWrappers
            Dim current As EdmWrappers = TryCast(HttpContext.Current.Session.Item("EdmMetadataHelps.EdmWrapperCollection.Current"), EdmWrappers)
            If (current Is Nothing) Then
                current = EdmWrappers.GetNewCurrent
                HttpContext.Current.Session.Item("EdmMetadataHelps.EdmWrapperCollection.Current") = current
            End If
            Return current
        End Function

        Private Shared Function GetThreadBoundCurrent() As EdmWrappers
            If (EdmWrappers._current Is Nothing) Then
                EdmWrappers._current = EdmWrappers.GetNewCurrent
            End If
            Return EdmWrappers._current
        End Function
        ''' <summary>
        ''' Get the current EdmWrapper collection
        ''' </summary>
        Public Shared ReadOnly Property Current() As EdmWrappers
            Get
                If EdmWrappers.SessionBound Then
                    Return EdmWrappers.GetSessionBoundCurrent
                End If
                Return EdmWrappers.GetThreadBoundCurrent
            End Get
        End Property
        ''' <summary>
        ''' Get whether the EdmWrapper is using a HttpSession
        ''' </summary>
        Private Shared ReadOnly Property SessionBound() As Boolean
            Get
                If Not EdmWrappers._sessionBound.HasValue Then
                    EdmWrappers._sessionBound = New Boolean?((Not HttpContext.Current Is Nothing))
                End If
                Return EdmWrappers._sessionBound.Value
            End Get
        End Property
        <ThreadStatic()> _
        Private Shared _current As EdmWrappers
        ''' <summary>
        ''' Whether the Current EdmWrapper is held in a HttpSession or not
        ''' </summary>
        ''' <remarks>if not held in the Session the current EdmWrapper is held in the ThreadStatic _current variable</remarks>
        Private Shared _sessionBound As Boolean?
    End Class
End Namespace

